package com.chen.pspms.controller;

import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.Holder;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.service.HolderSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    CompanyService companyService;
    @Autowired
    HolderSercice holderSercice;

    @RequestMapping("/list")
    public String findAll(Model model, HttpSession session){
        List<CompanyInfo> companylist = companyService.findAll();
        model.addAttribute("companylist",companylist);
        return "企业信息管理/企业信息管理页面";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        companyService.delete(id);
        return "redirect:/company/list";
    }

    @RequestMapping("/show")
    public String show(Integer id, Model model){
        //调用业务处理
        if(id==0) {
            //如果是新增，直接返回新增页面
            return "企业信息管理/新建";
        }else {
            //如果是修改，查询数据后返回
            CompanyInfo company = companyService.findyById(id);
            model.addAttribute("company", company);
        }
        //处理业务返回结果
        return "企业信息管理/修改";
    }

    @RequestMapping("/save")
    public String save(CompanyInfo company, Model model) {
        //调用业务处理
        String exist =null;
        if(companyService.findyByName(company.getName())!=null) {
            //如果员工合同表已存在
            exist="该企业已存在";
        }else {
            //合同不存在，则添加
            Holder holder= holderSercice.findyByName(company.getHolder().getName());
            company.setHolder(holder);
//            System.out.println(company);
            companyService.save(company);
        }
        //处理业务返回结果
        List<CompanyInfo> companylist = companyService.findAll();
        model.addAttribute("companylist", companylist);
        model.addAttribute("exist", exist);
        return "企业信息管理/企业信息管理页面";
    }

    @RequestMapping("/update")
    public String update(CompanyInfo company, Model model) {
        //调用业务处理
        Holder holder= holderSercice.findyByName(company.getHolder().getName());
        company.setHolder(holder);
        companyService.update(company);
        //处理业务返回结果
        return "redirect:/company/list";//重定向就是在字符串前面加上前�? redirect:
    }
}
